/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Builder;

import DAO.InformationFilter.CategoryDAO;
import DAO.InformationFilter.PageDAO;
import DAO.InformationFilter.PeriodClickDAO;
import DAO.InformationFilter.PrdDAO;
import DAO.InformationFilter.UsrCategoryClickDAO;
import DAO.InformationFilter.UsrClickDAO;
import DAO.InformationFilter.UserDAO;
import IUtil.Counter;
import Info.PageInfo;
import Info.PeriodInfo;
import Info.UserCategoryClickInfo;
import Info.UserInfo;
import MySqlAccess.RatingInfo;
import MySqlAccess.UserRating;
import gnu.trove.TIntArrayList;
import gnu.trove.TIntObjectHashMap;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class UsrCategoryClickBuilder
{
    public void Build()
    {
        PrdDAO periodDAO = PrdDAO.GetInstance();
        List<PeriodInfo> allPeriod = periodDAO.GetAllPeriod();

        UserDAO userDAO = new UserDAO();
        List<UserInfo> allUser = userDAO.GetAll();

        CategoryDAO categoryDAO = new CategoryDAO();
        List<Integer> allCatID = categoryDAO.GetAllID();

        PageDAO pageDAO = new PageDAO();

        UserRating rating = new UserRating();

        //////////////////////////////////////////////
        UsrCategoryClickDAO userCategoryClickDAO  = new UsrCategoryClickDAO();
        userCategoryClickDAO.DoEmpty();

        PeriodClickDAO periodClickDAO = PeriodClickDAO.GetInstance();
        periodClickDAO.DoEmpty();

        UsrClickDAO userClickDAO = new UsrClickDAO();
        userClickDAO.DoEmpty();
        ///////////////////////////////////////////////

        for (PeriodInfo currPeriod : allPeriod)
        {
            int periodClick = 0;

            for (UserInfo currUser : allUser)
            {
                List<RatingInfo> periodRating = rating.GetBy_User_Date(currUser.id, currPeriod.GetBegin(),
                        currPeriod.GetEnd());
                TIntObjectHashMap categoryClick = new TIntObjectHashMap();
                for (Integer currCategory : allCatID)
                {
                    categoryClick.put(currCategory, new Counter());
                }

                int userPeriodClick = 0;
                for (RatingInfo ratingInfo : periodRating)
                {
                    PageInfo pageInfo = pageDAO.GetByID(ratingInfo.m_idItem);
                    if (pageInfo != null)
                    {
                        Counter counter = (Counter)categoryClick.get(pageInfo.GetCategory());
                        counter.Increment();
                        userPeriodClick++;
                    }
                }               

                int userCategoryClick;
                for (Integer currCategory : allCatID)
                {
                    userCategoryClick = ((Counter)categoryClick.get(currCategory)).GetCounter();
                    UserCategoryClickInfo uccInfo = new UserCategoryClickInfo();
                    uccInfo.user = currUser.id;
                    uccInfo.period = currPeriod.GetID();
                    uccInfo.category = currCategory;
                    uccInfo.click = userCategoryClick;
                    if (userPeriodClick == 0)
                    {
                        uccInfo.percent = 0;
                    }
                    else
                    {
                        uccInfo.percent = userCategoryClick*1.0/userPeriodClick;
                    }
                    

                    userCategoryClickDAO.Insert(uccInfo);
                }
                periodClick += userPeriodClick;
                userClickDAO.Insert(currUser.id, currPeriod.GetID(), userPeriodClick);
            }
            periodClickDAO.Insert(currPeriod.GetID(), periodClick);

        }
    }

    public static void main(String[] argvs)
    {
        UsrCategoryClickBuilder builder = new UsrCategoryClickBuilder();
        builder.Build();
    }
}
